﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyD
{
    public partial class FormMyD : Form
    {
        private bool edited;
        private Cryptography crypt = new Cryptography();

        public FormMyD()
        {
            InitializeComponent();

            int backR = 50;
            int backG = 100;
            int backB = 200;

            new FocusColorHandler().TextBoxToPanel(txtSenha, panelChave, panelChave.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().RichTextBoxToPanel(txtConteudo, panelConteudo, panelChave.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().ListBoxToPanel(listArquivos, panelListArquivos, panelChave.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().PanelToPanel(panelToolbox2, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblFundoCor, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblFonteTipo, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblForeColor, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblBackColor, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblFont, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblFonteTipo, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().LabelToPanel(lblTextoCor, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().ButtonToPanel(bAplicar, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
            new FocusColorHandler().ButtonToPanel(bReset, panelToolbox, panelToolbox.BackColor, Color.FromArgb(backR, backG, backB));
        }

        private void FormMyD_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(GetLocalOfFiles()))
                Directory.CreateDirectory(GetLocalOfFiles());

            fileSaveDlg.InitialDirectory = GetLocalOfFiles();
            fileOpenDlg.InitialDirectory = GetLocalOfFiles();

            Atualizar();
        }

        private void bSalvar_Click(object sender, EventArgs e)
        {
            string day = DateTime.Now.Day.ToString() + "-";
            string month = DateTime.Now.Month.ToString() + "-";
            string year = DateTime.Now.Year.ToString();

            string file = day + month + year;

            fileSaveDlg.FileName = file + ".myd";

            if(fileSaveDlg.ShowDialog() == DialogResult.OK)
            {
                Salvar(fileSaveDlg.FileName);
            }
        }

        private void bCarregar_Click(object sender, EventArgs e)
        {
            if (fileOpenDlg.ShowDialog() == DialogResult.OK)
            {
                Ler(fileOpenDlg.FileName);
            }
        }

        private void bAtualizar_Click(object sender, EventArgs e)
        {
            Atualizar();
        }

        private string lastItem = "sem data.myd";

        private void listArquivos_SelectedIndexChanged(object sender, EventArgs e)
        {
            string lastLocal = GetLocalOfFiles() + "\\" + lastItem;
            string newLocal = GetLocalOfFiles() + "\\" + listArquivos.GetItemText(listArquivos.SelectedItem);

            lastItem = listArquivos.GetItemText(listArquivos.SelectedItem);

            if (edited)
            {
                if (MessageBox.Show("Deseja Salvar antes de carregar outro arquivo?", lastItem, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                {
                    Salvar(lastLocal);
                }
            }

            Ler(newLocal);
        }

        private void FormMyD_Activated(object sender, EventArgs e)
        {
            Atualizar();
        }

        private void txtConteudo_TextChanged(object sender, EventArgs e)
        {
            edited = true;
        }

        private void lblCor_Click(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;

            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                lbl.BackColor = colorDlg.Color;

                AplicarFormatacao();
            }
        }

        private void bAplicar_Click(object sender, EventArgs e)
        {
            AplicarFormatacao();
        }

        private void bReset_Click(object sender, EventArgs e)
        {
            lblBackColor.BackColor = Color.FromArgb(30, 30, 30);
            lblForeColor.BackColor = Color.Silver;

            lblFont.Font = new Font("Segoe UI", 11);

            AplicarFormatacao();
        }

        private void lblFont_Click(object sender, EventArgs e)
        {
            if (fontDlg.ShowDialog() == DialogResult.OK)
            {
                Font font = fontDlg.Font;

                lblFont.Font = font;
                lblFont.Text = font.Name + ";" + font.Size.ToString() + "pt";

                AplicarFormatacao();
            }
        }
    }
}